package org.jeecg.modules.xxgx.leqiht.controller;

import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.innerApi.leqiApi.util.LQRequestUtil;
import org.jeecg.modules.xxgx.leqiht.enums.LQhtYwlxEnum;
import org.jeecg.modules.xxgx.leqiht.service.ILeqiHtService;
import org.jeecg.modules.xxgx.leqiht.util.RequestWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Base64;

@Api(tags = "接收乐企发票回填信息")
@RestController
@RequestMapping("/leqi")
@Slf4j
public class LeqiFphtController {
    @Autowired
    private LQRequestUtil lqRequestUtil;
    @Autowired
    private ILeqiHtService leqiHtService;

    @ApiOperation(value = "乐企发票回填", notes = "乐企发票回填")
    @PostMapping(value = "/fpht")
    public Result<String> add(ServletRequest request, ServletResponse response) {
        RequestWrapper hsr = new RequestWrapper(request);
        JSONObject paramJson = getParamJson(hsr);
        LQhtYwlxEnum lQhtYwlxEnum = LQhtYwlxEnum.getObject(paramJson.getString("ywlx"));
        Result result = new Result();
        switch (lQhtYwlxEnum) {
            case LQFPBSWJJF:
                //版式文件回填
                result = leqiHtService.lqfpbswjjf(paramJson);
                break;
        }
        return result;
    }

    private JSONObject getParamJson(RequestWrapper request) {

        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader;
        InputStream inputStream;
        try {
            inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] charBuffer = new char[128];
                int bytesRead;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            }

            //密文转明文BASE64(AES(source))
            String source = stringBuilder.toString();

            //解密
            String secretSwitch = lqRequestUtil.aesKey;
            SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, secretSwitch.getBytes());
            source = new String(aes.decrypt(Base64.getDecoder().decode(source.getBytes())));
            request.setAesBody(source);

            return JSONObject.parseObject(source);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return new JSONObject();
    }
}
